/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.TCube;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import managedBean.TX.CustomerIOTRPortal.CustomerPointsMB;
import managedBean.TX.CustomerIOTRPortal.GroupBuyCustMB;
import managedBean.TX.CustomerIOTRPortal.ShoppingCartMB;
import managedBean.TX.CustomerIOTRPortal.UserPerformanceMB;
import managedBean.TX.SABM.StakeholderPointsMB;

/**
 *
 * @author Randall Chia
 */
@ManagedBean (name="paymentConfirmMB")
@ViewScoped
public class PaymentConfirmationMB {
    

    
    
    @ManagedProperty(value = "#{cartBean}")
    private ShoppingCartMB shoppingCartMB;
    
    @ManagedProperty(value = "#{groupBuyCustManagedBean}")
    private GroupBuyCustMB groupBuyCustMB;
    
    @ManagedProperty(value = "#{StakeholderPointsMB}")
    private StakeholderPointsMB stakeholderPointsMB;
        
    @ManagedProperty(value = "#{CustomerPointsMB}")
    private CustomerPointsMB customerPointsMB;
    
    @ManagedProperty(value = "#{userPerformanceMB}")
    private UserPerformanceMB userPerformanceMB;
    
    
    
    //This string holds the HTML content to be written on the payment complete page
    private String paymentCompleteMessage = "Thank you for shopping with us!";

    /** Creates a new instance of PaymentConfirmationMB */
    public PaymentConfirmationMB() {
    }
    
    
    
    
    public void doPaymentValidate(){
        System.out.println("returnURLMB: Redirected from Paypal to payment complete page, doing payment validation now...");
        
        System.out.println("returnURLMB: Attempting to check the transaction type...");
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        Object o = session.getAttribute("TransactionType");
        String transactionType;
        if(o == null){
            System.out.println("returnURLMB: ERROR, a transaction type was not specified in the session. No validation will be done.");
            return;
        }
        else{
            transactionType = (String)o;
            System.out.println("returnURLMB: Transaction Type retrieved from session. The type is: " + transactionType);
        }
        
        //Determine what kind of validation to perform based on the transaction type
        //The appropriate validation method will be called from the managed bean handling the transaction type   
        if(transactionType.equals("shoppingCartCheckout")){
            shoppingCartMB.validatePayment();   
            paymentCompleteMessage = shoppingCartMB.getPurchaseStatusMSG();
        }
        else if(transactionType.equals("buyGroupDeal")){
            groupBuyCustMB.validatePayment();
            paymentCompleteMessage = groupBuyCustMB.getPurchaseStatusMSG();
        }
        else if(transactionType.equals("userBuyPoints")){
            customerPointsMB.validatePayment();
            paymentCompleteMessage = customerPointsMB.getPurchaseStatusMSG();
        }
        else if(transactionType.equals("stakeholderBuyPoints")){
            stakeholderPointsMB.validatePayment();
            paymentCompleteMessage = stakeholderPointsMB.getPurchaseStatusMSG();
        }
        else if(transactionType.equals("buyPerformanceTickets")){
            userPerformanceMB.validatePayment();
            paymentCompleteMessage = userPerformanceMB.getPurchaseStatusMSG();
        }
    }
    
    
    /*
     * The payment complete button redirects to the Home page. Since Users and Stakeholders are both exposed to this
     * payment complete page, they will redirect to different place upon clicking the button.
     */
    public String getBackButtonURL(){
        String accountType;
        try {
            accountType = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("accountType");
        } catch (Exception ex) {
            ex.printStackTrace();
            return "/member/home.xhtml?faces-redirect=true";
        }
        
        if(accountType.equals("user")){
            return "/member/home.xhtml?faces-redirect=true";
        }
        else if(accountType.equals("representative")){
            return "/stakeholder/home.xhtml?faces-redirect=true";
        }
        else return "/member/home.xhtml?faces-redirect=true";
    }
    
    
    

    /**
     * @return the paymentCompleteMessage
     */
    public String getPaymentCompleteMessage() {
        return paymentCompleteMessage;
    }

    /**
     * @param paymentCompleteMessage the paymentCompleteMessage to set
     */
    public void setPaymentCompleteMessage(String paymentCompleteMessage) {
        this.paymentCompleteMessage = paymentCompleteMessage;
    }

    /**
     * @return the shoppingCartMB
     */
    public ShoppingCartMB getShoppingCartMB() {
        return shoppingCartMB;
    }

    /**
     * @param shoppingCartMB the shoppingCartMB to set
     */
    public void setShoppingCartMB(ShoppingCartMB shoppingCartMB) {
        this.shoppingCartMB = shoppingCartMB;
    }

    /**
     * @return the groupBuyCustMB
     */
    public GroupBuyCustMB getGroupBuyCustMB() {
        return groupBuyCustMB;
    }

    /**
     * @param groupBuyCustMB the groupBuyCustMB to set
     */
    public void setGroupBuyCustMB(GroupBuyCustMB groupBuyCustMB) {
        this.groupBuyCustMB = groupBuyCustMB;
    }

    /**
     * @return the stakeholderPointsMB
     */
    public StakeholderPointsMB getStakeholderPointsMB() {
        return stakeholderPointsMB;
    }

    /**
     * @param stakeholderPointsMB the stakeholderPointsMB to set
     */
    public void setStakeholderPointsMB(StakeholderPointsMB stakeholderPointsMB) {
        this.stakeholderPointsMB = stakeholderPointsMB;
    }

    /**
     * @return the customerPointsMB
     */
    public CustomerPointsMB getCustomerPointsMB() {
        return customerPointsMB;
    }

    /**
     * @param customerPointsMB the customerPointsMB to set
     */
    public void setCustomerPointsMB(CustomerPointsMB customerPointsMB) {
        this.customerPointsMB = customerPointsMB;
    }

    /**
     * @return the userPerformanceMB
     */
    public UserPerformanceMB getUserPerformanceMB() {
        return userPerformanceMB;
    }

    /**
     * @param userPerformanceMB the userPerformanceMB to set
     */
    public void setUserPerformanceMB(UserPerformanceMB userPerformanceMB) {
        this.userPerformanceMB = userPerformanceMB;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
}// End Class
